home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 40 / Amiga Format CD40 (1999-05-11)(Future Publishing)(GB)(Track 1 of 3)[!][issue 1999-06].iso / -readerstuff- / paul_qureshi / formats / lwstuff.lzx / lw.txt next >
Text File  |  1999-02-04  |  3KB  |  101 lines

  1.  
  2. Here are some simplified code fragments showing how LightWave computes UV
  3. coordinates from X, Y, and Z.  If the resulting UV coordinates are not in
  4. the range from 0 to 1, the appropriate integer should be added to them to
  5. bring them into that range (the fract function should have accomplished
  6. this by subtracting the floor of each number from itself).  Then they can
  7. be multiplied by the width and height (in pixels) of an image map to
  8. determine which pixel to look up.  The texture size, center, and tiling
  9. parameters are taken right off the texture control panel.
  10.  
  11.  
  12.     x -= xTextureCenter;
  13.     y -= yTextureCenter;
  14.     z -= zTextureCenter;
  15.     if (textureType == TT_PLANAR) {
  16.         s = (textureAxis == TA_X) ? z / zTextureSize + .5 :
  17.           x / xTextureSize + .5;
  18.         t = (textureAxis == TA_Y) ? -z / zTextureSize + .5 :
  19.           -y / yTextureSize + .5;
  20.         u = fract(s);
  21.         v = fract(t);
  22.     }
  23.     else if (type == TT_CYLINDRICAL) {
  24.         if (textureAxis == TA_X) {
  25.             xyztoh(z,x,-y,&lon);
  26.             t = -x / xTextureSize + .5;
  27.         }
  28.         else if (textureAxis == TA_Y) {
  29.             xyztoh(-x,y,z,&lon);
  30.             t = -y / yTextureSize + .5;
  31.         }
  32.         else {
  33.             xyztoh(-x,z,-y,&lon);
  34.             t = -z / zTextureSize + .5;
  35.         }
  36.         lon = 1.0 - lon / TWOPI;
  37.         if (widthTiling != 1.0)
  38.             lon = fract(lon) * widthTiling;
  39.         u = fract(lon);
  40.         v = fract(t);
  41.     }
  42.     else if (type == TT_SPHERICAL) {
  43.         if (textureAxis == TA_X)
  44.             xyztohp(z,x,-y,&lon,&lat);
  45.         else if (textureAxis == TA_Y)
  46.             xyztohp(-x,y,z,&lon,&lat);
  47.         else
  48.             xyztohp(-x,z,-y,&lon,&lat);
  49.         lon = 1.0 - lon / TWOPI;
  50.         lat = .5 - lat / PI;
  51.         if (widthTiling != 1.0)
  52.             lon = fract(lon) * widthTiling;
  53.         if (heightTiling != 1.0)
  54.             lat = fract(lat) * heightTiling;
  55.         u = fract(lon);
  56.         v = fract(lat);
  57.     }
  58.  
  59. support functions:
  60.  
  61. void xyztoh(float x,float y,float z,float *h)
  62. {
  63.     if (x == 0.0 && z == 0.0)
  64.         *h = 0.0;
  65.     else {
  66.         if (z == 0.0)
  67.             *h = (x < 0.0) ? HALFPI : -HALFPI;
  68.         else if (z < 0.0)
  69.             *h = -atan(x / z) + PI;
  70.         else
  71.             *h = -atan(x / z);
  72.     }
  73. }
  74.  
  75. void xyztohp(float x,float y,float z,float *h,float *p)
  76. {
  77.     if (x == 0.0 && z == 0.0) {
  78.         *h = 0.0;
  79.         if (y != 0.0)
  80.             *p = (y < 0.0) ? -HALFPI : HALFPI;
  81.         else
  82.             *p = 0.0;
  83.     }
  84.     else {
  85.         if (z == 0.0)
  86.             *h = (x < 0.0) ? HALFPI : -HALFPI;
  87.         else if (z < 0.0)
  88.             *h = -atan(x / z) + PI;
  89.         else
  90.             *h = -atan(x / z);
  91.         x = sqrt(x * x + z * z);
  92.         if (x == 0.0)
  93.             *p = (y < 0.0) ? -HALFPI : HALFPI;
  94.         else
  95.             *p = atan(y / x);
  96.     }
  97. }
  98.  
  99.  
  100.  
  101.